//
//  EmotionToolBar.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/27.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

enum EmotionToolBarType: Int{
    
    case Recent = 0,Default,Emoji,Lxh
}

class EmotionToolBar: UIStackView {
    
    //提供给外界设置当前选中按钮index的属性
    var selectedSection: Int = 0{
        
        didSet{
            
            let button = self.viewWithTag(selectedSection) as! UIButton

            if button != lastSelectedButton{
                
                button.selected = true
                lastSelectedButton?.selected = false
                //记录按钮的状态
                lastSelectedButton = button
            }
        }
    }
    
    //定义闭包
    var childButtonClickClosure: ((type: EmotionToolBarType) -> ())?
    
    var lastSelectedButton: UIButton?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        self.tag = 20000
        self.distribution = .FillEqually
        
        addChildButtons("最近", imgName: "compose_emotion_table_left",type:.Recent)
        addChildButtons("默认", imgName: "compose_emotion_table_mid",type:.Default)
        addChildButtons("Emoji", imgName: "compose_emotion_table_mid",type:.Emoji)
        addChildButtons("浪小花", imgName: "compose_emotion_table_right",type:.Lxh)
    }

    private func addChildButtons(text: String,imgName: String,type: EmotionToolBarType){
        
        let button = UIButton()
        
        //点击监听事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //记录按钮的tag
        button.tag = type.rawValue
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage.scalePic(imgName + "_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage.scalePic(imgName + "_selected"), forState: UIControlState.Selected)
        
        button.setTitle(text, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        addArrangedSubview(button)
    }
    
    @objc private func childButtonClick(button: UIButton){
        
        if lastSelectedButton == button {
            
            return
        }
        
        //执行闭包
        childButtonClickClosure?(type: EmotionToolBarType(rawValue: button.tag)!)
        
//        print("childButtonClick")
        //让上一个按钮取消选中状态
        lastSelectedButton?.selected = false
        
        //让点击的按钮选中
        button.selected = true
        
        //记录下当前按钮的状态
        lastSelectedButton = button
        
//        //执行闭包
//        childButtonClickClosure?(type: EmotionToolBarType(rawValue: button.tag)!)
    }
}
